/**
 * Created by Hou on 2016/3/18.
 */
app.service('ajax2', ['$q', '$http', 'SERVER_URL', 'toolService', '$state', '$sessionStorage', 'Flash', function ($q, $http, SERVER_URL, toolService, $state, $sessionStorage, Flash) {
    this.post = function (dataInput) {
        var req = {
            method: 'POST',
            url: SERVER_URL + dataInput.url,
            data: dataInput.data,
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            transformRequest: function (obj) {
                var str = [];
                for (var p in obj)
                    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                return str.join("&");
            }
        };
        return httpReq(req, dataInput);
    };
    this.get = function (dataInput) {
        var req = {
            method: 'GET',
            url: SERVER_URL + dataInput.url,
            params: dataInput.data
    };
        return httpReq(req, dataInput);
    };
    function httpReq(req, dataInput) {
        if (dataInput.toast) {
            toast.create(dataInput.toast);
        }
        var deferred = $q.defer();
        $http(req).success(function (response, status, headers, config) {
            var data = response.data;
            var code = response.code;
            if (code == "0") {
                deferred.resolve(data, status, headers, config);
            } else if (code == 1002 || code == 1003) {
                delete $sessionStorage.username;
                $state.go("login");
            } else {
                Flash.create('warning', response.msg, 'custom-class');
            }

        }).error(function (response, status, headers, config) {
            Flash.create('danger', '请求异常', 'custom-class');
            deferred.reject(data, status, headers, config);
        });
        return deferred.promise;
    }
}]);
/**
 * Created by Laggo on 15/12/11.
 */
/**
 * Created by Laggo on 15/12/11.
 */
